package cn.keniykey.mc.viewlibrary.container.api;

import javax.annotation.Nullable;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public final class ContainerClickReport {
	private final Player player;
	private final Container container;
	private final EClickType clicktype;
	private final int slot;
	
	public ContainerClickReport(Player player1,Container container1,EClickType clicktype1,int slot1) {
		this.player = player1;
		this.container = container1;
		this.clicktype = clicktype1;
		this.slot = slot1;
	}
	
	public Player getPlayer() {
		return this.player;
	}
	public Container getContainer() {
		return this.container;
	}
	public EClickType getEClickType() {
		return this.clicktype;
	}
	public int getClickSlot() {
		return this.slot;
	}
	public boolean isClickContainer() {
		return (this.slot >= 0 && this.slot < this.container.getContainerSize());
	}
	public boolean isClickPlayerBackpack() {
		return this.slot >= this.container.getContainerSize();
	}
	@Nullable
	public ItemStack getCursorItemStack() {
		if(this.slot >= 0) return getItemStackFromSlot(this.player, this.container, this.slot);
		return null;
	}
	/**
	 * @param slot 必须大于等于0
	 */
	@Nullable
	public static ItemStack getItemStackFromSlot(Player player, Container container, int slot) {
		if(slot >= container.getContainerSize()) {
			slot -= container.getContainerSize();
			slot = (slot >= 27 ? slot - 27 : slot + 9);
			return player.getInventory().getItem(slot);
		}
		return container.getItemStack(slot);
	}
	/**
	 * @param slot 必须大于等于0
	 */
	public static void setItemStackWithSlot(Player player, Container container, int slot,@Nullable ItemStack item) {
		if(slot >= container.getContainerSize()) {
			slot -= container.getContainerSize();
			slot = (slot >= 27 ? slot - 27 : slot + 9);
			player.getInventory().setItem(slot, item);
			return;
		}
		if(item != null && item.getType().equals(Material.AIR )) item = null;
		container.setItemStack(slot, item, true);
	}
}